﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GanagroLiteData;
using Csla.Validation;
using GeneralUtilities;

namespace GanagroLiteBO
{
    public partial class Operacion_BO 
    {
        private OPERACIONE currentOperacionDO;
        protected List<string> Contenedor;
//        private bool _validationrulesSuspended = false;

        #region Additional Properties

        internal string _CODIGO;	

        public string CODIGO
        {
            get { return _CODIGO; }
            set
            {
                if (_CODIGO != value)
                {
                    _CODIGO = value;
                    FirePropertyChangedNotification("CODIGO");
                }
            }
        }

        private string _nota;

        public string Nota
        {
            get { return _nota; }
            set
            {
                if (_nota != value)
                {
                    _nota = value;
                    FirePropertyChangedNotification("Nota");
                }
            }
        }

        public string OPERACION_DESC
        {
            get { return utilities.GetDescOperacion((TiposOperacion) _OPERACION); } 
        }

        #endregion

        #region constructors

        public Operacion_BO()
        {
            AddBusinessRules();
        }

        public Operacion_BO(bool supressBusinessRules)
        {
            if (supressBusinessRules == false)
            {
                AddBusinessRules();
            }
        }

        public Operacion_BO(string codigo, DateTime dt, TiposOperacion top, short? Peso,int ID)
        {
            currentOperacionDO = new OPERACIONE();
            currentOperacionDO.OPERACION = (short)top;
            currentOperacionDO.CODIGO = codigo;
            currentOperacionDO.FECHA = dt;
            currentOperacionDO.VALOR1 = (short)Peso;
            currentOperacionDO.ID = ID;

            this._OPERACION = (short)top;
            this._VALOR1 = Peso;
            this._FECHA = dt;
            this._CODIGO = codigo;
            this._ID = ID;
        }

        #endregion
 
        #region public methods

        public void AddOperationBusinessRules()
        {
            AddBusinessRules();
        }

        public bool IsValid()
        {
           return IsValid(false);
        }


        public bool IsValid(bool ByPassRuleChecking)
        {
            if (ByPassRuleChecking == false)
            {
                ValidationRules.CheckRules();
            }

            return (ValidationRules.IsValid);
        }


        public void Guardar()
        {
            using (GlContext gContext = new GlContext())
            {
                currentOperacionDO = new OPERACIONE();
                utilities.CopyProperties(this, currentOperacionDO, null);
                currentOperacionDO.ID = FindMaxID();
                gContext.OPERACIONES.Add(currentOperacionDO);
                gContext.SaveChanges();
            }
        }

        public void Delete()
        {
            using (GlContext gContext = new GlContext())
            {
                gContext.OPERACIONES.Remove(currentOperacionDO);
                gContext.SaveChanges();
            }
        }

        private static int FindMaxID()
        {
            return  int.Parse(UtilityDB.GetVal("SELECT MAX(ID) MAXID FROM OPERACIONES"));
        }

        #endregion

        #region Validation Rules
        private ValidationRules _validationRules;

        public ValidationRules ValidationRules
        {
            get
            {
                if (_validationRules == null)
                    _validationRules = new ValidationRules(this);
                return _validationRules;
            }
        }

        protected static bool CodigoEnContenedor<T>(T target, Csla.Validation.RuleArgs e) where T : Operacion_BO
        {
            bool isvalid = true;

            if (target.Contenedor == null)
            {
                return true;
            }

            isvalid = utilities.NoDuplicates(target.CODIGO, target.Contenedor);
            if (isvalid == false)
            {
                e.Description = "Codigo ya esta definido en este grupo";
            }
            return isvalid;
        }

        protected static bool FechaValida<T>(T target, Csla.Validation.RuleArgs e) where T : Operacion_BO
        {
           return utilities.EsFechaValida(target.CODIGO, target.FECHA, target.OPERACION, e);
        }

        protected void AddBusinessRules()
        {
            try
            {
                ValidationRules.AddRule(new Csla.Validation.RuleHandler<Operacion_BO, RuleArgs>(CodigoEnContenedor), "CODIGO",0);
                ValidationRules.AddRule(new Csla.Validation.RuleHandler<Operacion_BO, RuleArgs>(FechaValida), "CODIGO",3);
                ValidationRules.AddRule(new Csla.Validation.RuleHandler<Operacion_BO, RuleArgs>(FechaValida), "FECHA",3);
            }
            catch (Exception ex)
            {
                throw (new Exception("AddBusinessRules: " + ex.Message, ex));
            }
        }

        #endregion

    }

    public class Operaciones : BindingListView<Operacion_BO>
    {
        public Operaciones()
        {
        }

    }

}
